home *** CD-ROM | disk | FTP | other *** search
/ PCMania 64 / PCMania CD64_1.iso / phy / phy006 / lands / landsc.c next >
C/C++ Source or Header  |  1997-06-12  |  7KB  |  203 lines

  1. /*══════════════════════════════════════════════════════════════════════════
  2.   LandScap.c 
  3.   Creación y visualización de un LandsCape de 256x200.
  4.   Coded by [NoP] of Compiler SoftWare.
  5.   
  6.   Aviso que no está nada optimizado para facilitar su lectura... :)
  7.   Además he intentado no poner ASM para aquellos que no tenga tasm.
  8.  ══════════════════════════════════════════════════════════════════════════*/
  9.  
  10. #include <conio.h>
  11. #include <dos.h>
  12. #include <stdlib.h>
  13. #include <alloc.h>
  14. #include <time.h>
  15.  
  16. void SetVideoMode( char );
  17. void PutPixel( int, int, char );
  18. void SetPalette( unsigned char * );
  19.  
  20. char CalcAverage( int, int, int );
  21. void Plasma( int, int, int, int, unsigned char * );
  22. void RotatePalette ( unsigned char * );
  23. void RotatePlasmaPalette ( void );
  24. void BlurringFilter( void );
  25.  
  26. unsigned char *Mapa;
  27.  
  28. /* File created with  BIN2TXT  v1.0  by  NOP of Compiler SoftWare */
  29. unsigned char LandScapePalette[ 768 ] = { 
  30.       0,0,0,0,0,25,0,0,25,0,0,26,0,0,27,0,0,28,0,0,
  31.       28,0,0,29,0,0,30,0,0,31,0,0,32,0,0,32,0,0,33,0,
  32.       0,34,0,0,35,0,0,36,0,0,36,0,0,37,0,0,38,0,0,39,
  33.       0,0,39,0,0,40,0,0,41,0,0,42,0,0,43,0,0,43,0,0,
  34.       44,0,0,45,0,0,46,0,0,47,0,0,47,0,0,48,0,0,49,0,
  35.       0,50,0,0,51,0,0,51,0,0,52,0,0,53,0,0,54,0,0,54,
  36.       0,0,55,0,0,56,0,0,57,0,0,58,0,0,58,0,0,59,0,0,
  37.       60,0,0,61,0,0,62,4,4,63,7,7,63,11,11,63,15,15,63,18,
  38.       18,63,22,22,63,26,26,63,29,29,63,33,33,63,37,37,63,40,40,63,
  39.       44,44,63,48,48,63,51,51,63,55,55,63,59,59,63,50,50,0,46,50,
  40.       0,43,51,0,40,52,0,37,53,0,34,54,0,31,54,0,28,55,0,25,
  41.       56,0,21,57,0,18,58,0,15,58,0,12,59,0,9,60,0,6,61,0,
  42.       3,62,0,0,63,0,0,62,0,1,62,0,1,61,0,2,61,0,2,60,
  43.       0,3,60,0,3,59,0,4,59,0,4,58,0,5,58,0,5,57,0,6,
  44.       57,0,6,56,0,7,56,0,7,55,0,8,55,0,8,54,0,9,54,0,
  45.       9,53,0,10,53,0,10,52,0,11,52,0,11,51,0,12,51,0,12,50,
  46.       0,13,50,0,13,49,0,14,49,0,14,48,0,15,48,0,15,47,0,16,
  47.       47,0,16,46,0,17,46,0,17,45,0,18,45,0,18,44,0,19,44,0,
  48.       19,43,0,20,43,0,20,42,0,21,42,0,21,41,0,22,41,0,22,40,
  49.       0,23,40,0,23,39,0,24,39,0,24,38,0,25,38,0,25,37,0,26,
  50.       37,0,26,36,0,27,36,0,27,35,0,28,35,0,28,34,0,29,34,0,
  51.       29,33,0,30,33,0,30,32,0,31,32,0,31,31,0,31,31,0,31,31,
  52.       1,32,32,2,32,32,3,33,33,4,33,33,5,34,34,6,34,34,7,35,
  53.       35,8,35,35,9,36,36,10,36,36,11,37,37,12,37,37,13,38,38,14,
  54.       38,38,15,39,39,16,39,39,17,40,40,18,40,40,19,41,41,20,41,41,
  55.       21,42,42,22,42,42,23,43,43,24,43,43,25,44,44,26,44,44,27,45,
  56.       45,28,45,45,29,46,46,30,46,46,31,47,47,32,47,47,33,48,48,34,
  57.       48,48,35,49,49,36,49,49,37,50,50,38,50,50,39,51,51,40,51,51,
  58.       41,52,52,42,52,52,43,53,53,44,53,53,45,54,54,46,54,54,47,0,
  59.       63,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  60.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  61.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  62.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  63.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  64.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  65.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  66.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  67.       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  68.       0,0,0,0,0,0,0,0
  69.  };
  70.  
  71. /*─────────────────────────────────────────────────────────────────────────*/
  72. void main()
  73. {
  74. int x, y; 
  75.  
  76.  
  77.   Mapa = (unsigned char *) calloc(256*200, 1);         /* asignar memoria */ 
  78.  
  79.   if ( Mapa == NULL )
  80.    {
  81.     cprintf("\aNo hay suficiente memoria para el buffer de 256*200.\r\n");
  82.     exit(1);
  83.    }
  84.  
  85.  cprintf("Generating 256x200 plasma landscape...");
  86.  randomize();
  87.  
  88.  Mapa[0] = 128;
  89.  BlurringFilter();
  90.  Plasma( 0, 0, 255, 199, Mapa );
  91.  
  92.  SetVideoMode(0x13);
  93.  SetPalette( LandScapePalette );
  94.  
  95.  for( y=0; y<200; y++ )
  96.   for( x=0; x<256; x++ )
  97.    PutPixel( x, y, Mapa[(y*256)+x] );
  98.  
  99.  getch();
  100.  
  101.  SetVideoMode(0x3);
  102.  free(Mapa);                                         /* liberar memoria */
  103.  
  104. }
  105.  
  106. /*─────────────────────────────────────────────────────────────────────────*/
  107. void SetVideoMode( char modo )
  108. {
  109. /* me parece que es la 1ª vez que uso esto en vez de asm... X'DDD */
  110.  union REGS regs;
  111.  
  112.   regs.h.ah = 0;              /* iniciar modo de video */
  113.   regs.h.al = modo;
  114.   int86(0x10, ®s, ®s);
  115.  
  116. }
  117.  
  118. /*─────────────────────────────────────────────────────────────────────────*/
  119. void SetPalette( unsigned char *Paleta )
  120. {
  121.   int contador;
  122.  
  123.   outportb( 0x3c8, 0 );
  124.     
  125.   for( contador=0; contador<256*3; contador++ )
  126.     outportb( 0x3c9, Paleta[contador] );
  127.  
  128. }
  129.  
  130. /*─────────────────────────────────────────────────────────────────────────*/
  131. void PutPixel( int x, int y, char color )
  132. {
  133.   pokeb(0xA000, (320*y)+x, color );
  134. }
  135.  
  136. /*─────────────────────────────────────────────────────────────────────────*/
  137. char CalcAverage( int value, int noise, int elements )
  138. {
  139.    int average;
  140.        average = (value+noise-random(noise*2)) / elements;
  141.        
  142.        if (average>192) average = (average%192)+1; 
  143.        else if (average<5) average = 5;
  144.  
  145.        return(average);
  146. }
  147.  
  148. /*─────────────────────────────────────────────────────────────────────────*/
  149. void Plasma( int x1, int y1, int x2, int y2, unsigned char *Mapa )
  150. {
  151.   int XMedia, YMedia, Valor1, Valor2, Valor3, Valor4, Noise;
  152.  
  153.     if( (x2-x1)<2 && (y2-y1)<2 ) return;                         /* paso 1 */
  154.     
  155.       XMedia = (x2+x1)/2  ;
  156.       YMedia = (y2+y1)/2  ;                                      /* paso 2 */
  157.  
  158.       Noise = 5*(x2-x1+y2-y1)/3;                                 /* paso 3 */
  159.  
  160.       Valor1 = Mapa[ (y1*256) + x1 ];
  161.       Valor2 = Mapa[ (y2*256) + x1 ];
  162.       Valor3 = Mapa[ (y1*256) + x2 ];
  163.       Valor4 = Mapa[ (y2*256) + x2 ];                            /* paso 4 */
  164.             
  165.       if( Mapa[ (y1*256)+ XMedia] == 0 )                         /* paso 5 */
  166.             Mapa[ (y1*256)+ XMedia] = CalcAverage( Valor1+Valor3, Noise, 2 );
  167.       if( Mapa[(YMedia*256)+ x1] == 0 )
  168.             Mapa[(YMedia*256)+ x1] = CalcAverage( Valor1+Valor2, Noise, 2 );
  169.       if( Mapa[(YMedia*256)+ x2] == 0 )
  170.             Mapa[(YMedia*256)+ x2] = CalcAverage( Valor3+Valor4, Noise, 2 );
  171.       if( Mapa[(y2*256)+ XMedia] == 0 )
  172.             Mapa[(y2*256)+ XMedia] = CalcAverage( Valor2+Valor4, Noise, 2 );
  173.  
  174.       Mapa[(YMedia*256)+XMedia] =                                /* paso 6 */
  175.         CalcAverage( Valor1+Valor2+Valor3+Valor4, Noise, 4 );
  176.  
  177.       Plasma( x1, y1, XMedia, YMedia, Mapa );                    /* paso 7 */
  178.       Plasma( XMedia, y1, x2, YMedia, Mapa );
  179.       Plasma( x1, YMedia, XMedia, y2, Mapa );
  180.       Plasma( XMedia, YMedia, x2, y2, Mapa );
  181. }
  182.  
  183.  
  184. /*─────────────────────────────────────────────────────────────────────────*/
  185. #define  NUM_POINTS    10000
  186. #define  K1             4/10
  187. #define  K2             3/10
  188. #define  K3             3/10                             /* K1+K2+K3 = 1 */
  189.  
  190. void BlurringFilter( void )
  191. {
  192.       int bucle, x, y;
  193.  
  194.        for( bucle=0; bucle<NUM_POINTS; bucle++ )
  195.         {
  196.          x = 5+random(250);
  197.          y = 5+random(195);                  /* cogemos puntos aleatorios */
  198.          Mapa[256*y+x] = Mapa[256*y+x]*K1 + 
  199.                          Mapa[256*(y-2)+(x+3)]*K2 + 
  200.                          Mapa[256*(y-4)+(x+2)]*K3;
  201.         }
  202. }
  203.